<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Modules\Car\Entities\CarOrder;
use Illuminate\Support\Facades\DB;
use App\Models\RevenueStatement;
use App\Models\Car\ExternalTransfer;
use Carbon\Carbon;

class CreateRevenueData implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    protected $order;
    public function __construct()
    {
        //
       // $this->order = $order;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        // 取出所有已结算订单
        $ordersett = CarOrder::query()->where('status', 6)->whereIn('process_status',[4,5,6])->where('fact_pay','>', 0)->select('department','created_at', 'fact_pay')->get()->toArray();
        // 取出外调订单
        $firstCollect = ExternalTransfer::query()->select(
                        DB::raw('department_id as department'),
                        DB::raw('created_at'),
                        DB::raw('profit_price as fact_pay'),
                    )->get()->toArray();
        $ordersettdate = [];
        for ($i=0; $i < count($ordersett); $i++) {
            $date = Carbon::parse($ordersett[$i]['created_at']);
            $ordersett[$i]['year'] = $date->year;
            $ordersett[$i]['month'] = $date->month;
            $ordersettdate[] = [
                    'department'=>$ordersett[$i]['department'],
                    'fact_pay'=>0,
                    'month'=>$ordersett[$i]['month'],
                    'year'=>$ordersett[$i]['year'],
                    'keys'=>$ordersett[$i]['department'] . $ordersett[$i]['year'] . $ordersett[$i]['month']
            ];
        }
        for ($o=0; $o < count($firstCollect); $o++) {
            $date = Carbon::parse($firstCollect[$o]['created_at']);
            $firstCollect[$o]['year'] = $date->year;
            $firstCollect[$o]['month'] = $date->month;
            $ordersettdate[] = [
                    'department'=>$firstCollect[$o]['department'],
                    'fact_pay'=>0,
                    'month'=>$firstCollect[$o]['month'],
                    'year'=>$firstCollect[$o]['year'],
                    'keys'=>$firstCollect[$o]['department'] . $firstCollect[$o]['year'] . $firstCollect[$o]['month']
            ];
        }
        $collection = collect($ordersettdate);
        $uniquelist = $collection->unique('keys')->values()->all();
        for ($j=0; $j < count($ordersett); $j++) {
            $keys = $ordersett[$j]['department'] . $ordersett[$j]['year'] . $ordersett[$j]['month'];        
            $index = array_search($keys, array_column($uniquelist, 'keys'));   
            $uniquelist[$index]['fact_pay'] = $uniquelist[$index]['fact_pay']+$ordersett[$j]['fact_pay'];
        }
        for ($p=0; $p < count($firstCollect); $p++) {
            $keys = $firstCollect[$p]['department'] . $firstCollect[$p]['year'] . $firstCollect[$p]['month'];        
            $index = array_search($keys, array_column($uniquelist, 'keys'));   
            $uniquelist[$index]['fact_pay'] = $uniquelist[$index]['fact_pay']+$firstCollect[$p]['fact_pay'];
        }
        for ($n=0; $n < count($uniquelist); $n++) {
            // 更新到营收表（实收租金：fact_pay）
            RevenueStatement::query()->where('department_id', $uniquelist[$n]['department'])->where('year', $uniquelist[$n]['year'])->where('month', $uniquelist[$n]['month'])->update(['income'=>$uniquelist[$n]['fact_pay']]);
        }
    }
}
